package com.tiger.admin.http.common.dictionary;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiger.admin.Db;
import com.tiger.admin.SaveOperateLog;
import com.tiger.admin.http.common.dictionary.dto.AddItem;
import com.tiger.admin.http.common.dictionary.dto.UpdateItem;
import com.tiger.db.entity.DictionaryItem;
import com.tiger.utils.ResponseUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/dictionary/item")
public class Item {
    private final Db db;

    public Item(Db db) {
        this.db = db;
    }

    @SaveOperateLog("新增字典")
    @PostMapping("add")
    public Object add(@RequestBody @Validated AddItem data) {
        Map<String, Object> where = new HashMap<>();
        where.put("parent_id", data.getParentId());
        where.put("category_id", data.getCategoryId());

        if(db.dictionaryItem.exists(new QueryWrapper<DictionaryItem>().select("id").eq("name", data.getName()).allEq(where))) {
            return ResponseUtil.failRes("已存在相同的名称");
        }

        if(db.dictionaryItem.exists(new QueryWrapper<DictionaryItem>().select("id").eq("code", data.getCode()).allEq(where))) {
            return ResponseUtil.failRes("已存在相同的编码");
        }

        DictionaryItem item = new DictionaryItem();
        item.setName(data.getName());
        item.setCode(data.getCode());
        item.setCategoryId(data.getCategoryId());
        item.setParentId(data.getParentId());

        boolean r = db.dictionaryItem.save(item);


        return r ? ResponseUtil.successRes(item) : ResponseUtil.failRes();
    }


    @SaveOperateLog("更新字典")
    @PostMapping("update/{id}")
    public Object add(@RequestBody @Validated UpdateItem data, @PathVariable long id) {
        DictionaryItem row = db.dictionaryItem.getOne(new QueryWrapper<DictionaryItem>().select("name", "code", "parent_id").eq("id", id));
        if(row == null) return ResponseUtil.failRes();

        if(!Objects.equals(data.getName(), row.getName())) {
            if(db.dictionaryItem.exists(new QueryWrapper<DictionaryItem>().eq("name", data.getName()).eq("parent_id", row.getParentId()))) {
                return ResponseUtil.failRes("已存在相同名称");
            }
        }

        if(!Objects.equals(data.getCode(), row.getCode())) {
            if(db.dictionaryItem.exists(new QueryWrapper<DictionaryItem>().eq("code", data.getCode()).eq("parent_id", row.getParentId()))) {
                return ResponseUtil.failRes("已存在相同编码");
            }
        }

        DictionaryItem updateData = new DictionaryItem();
        updateData.setName(data.getName());
        updateData.setCode(data.getCode());
        updateData.setId(id);

        boolean r = db.dictionaryItem.updateById(updateData);

        return r ? ResponseUtil.successRes(updateData) : ResponseUtil.failRes();
    }

    @SaveOperateLog("删除字典")
    @PostMapping("delete/{id}")
    public Object del(@PathVariable long id) {
        boolean r = db.dictionaryItem.removeById(id);
        return r ? ResponseUtil.successRes() : ResponseUtil.failRes();
    }


    @GetMapping("{category}")
    public Object items(@PathVariable String category) {
        List<DictionaryItem> list = db.dictionaryItem.getItemsByCategory(category);

        return ResponseUtil.successRes(list);
    }

}
